/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.sxyc.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.Result;
import io.renren.modules.yckj.common.ThreadLocalUtil;
import io.renren.modules.yckj.dto.CouponsDTO;
import io.renren.modules.yckj.dto.CouponsLogsDTO;
import io.renren.modules.yckj.entity.CouponsEntity;
import io.renren.modules.yckj.enums.status.CouponsStatus;
import io.renren.modules.yckj.service.CouponsLogsService;
import io.renren.modules.yckj.service.CouponsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/yckj/coupons")
@Tag(name = "优惠券接口", description = "优惠券接口")
@AllArgsConstructor
public class ApiCouponsController {

    private final CouponsService couponsService;
    private final CouponsLogsService couponsLogsService;

    @Login
    @GetMapping("receive")
    @Operation(summary = "领取")
    @ApiResponse(content = @Content(mediaType = "application/json"))
    @Parameter(name = "couponsId", description = "优惠券Id", required = true)
    public Result<Boolean> receive(@RequestParam("couponsId") Long couponsId) throws Exception {
        CouponsDTO couponsDTO = couponsService.get(couponsId);
        if (couponsDTO != null) {
            if (couponsDTO.getReceived().equals(couponsDTO.getNum())) {
                return new Result<Boolean>().error("优惠券已领取完");
            } else {
                CouponsLogsDTO couponsLogsDTO = ConvertUtils.sourceToTarget(couponsDTO, CouponsLogsDTO.class);
                couponsLogsDTO.setId(null);
                couponsLogsDTO.setCouponsId(couponsId);
                couponsLogsDTO.setUserId(ThreadLocalUtil.getUserId());
                couponsLogsDTO.setUsername(ThreadLocalUtil.getUserName());
                couponsLogsDTO.setCreateTime(new Date());
                couponsLogsDTO.setUpdateTime(new Date());

                CouponsEntity couponsEntity = new CouponsEntity();
                couponsEntity.setId(couponsId);
                couponsEntity.setReceived(couponsDTO.getReceived() + 1);
                boolean save = couponsLogsService.receive(couponsEntity, couponsLogsDTO);

                return new Result<Boolean>().ok(save);
            }
        } else {
            return new Result<Boolean>().error("优惠券不存在");
        }
    }

    @Login
    @GetMapping("receive/page")
    @Operation(summary = "可领取列表")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = CouponsDTO.class)))
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", required = true),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)")
    })
    public Result<PageData<CouponsDTO>> receivePage(@RequestParam Map<String, Object> params) {
        params.put("userId", ThreadLocalUtil.getUserId());
        PageData<CouponsDTO> page = couponsService.pageForApi(params);
        return new Result<PageData<CouponsDTO>>().ok(page);
    }

    @Login
    @GetMapping("available/page")
    @Operation(summary = "可用列表")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = CouponsLogsDTO.class)))
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", required = true),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)")
    })
    public Result<PageData<CouponsLogsDTO>> availablePage(@RequestParam Map<String, Object> params) {
        params.put("userId", ThreadLocalUtil.getUserId());
        params.put("available", true);
        params.put("status", CouponsStatus.START.getCode());
        PageData<CouponsLogsDTO> page = couponsLogsService.page(params);
        return new Result<PageData<CouponsLogsDTO>>().ok(page);
    }

    @Login
    @GetMapping("used/page")
    @Operation(summary = "已用列表")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = CouponsLogsDTO.class)))
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", required = true),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)")
    })
    public Result<PageData<CouponsLogsDTO>> usedPage(@RequestParam Map<String, Object> params) {
        params.put("userId", ThreadLocalUtil.getUserId());
        params.put("status", CouponsStatus.FINISH.getCode());
        PageData<CouponsLogsDTO> page = couponsLogsService.page(params);
        return new Result<PageData<CouponsLogsDTO>>().ok(page);
    }

    @Login
    @GetMapping("expire/page")
    @Operation(summary = "失效列表")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = CouponsLogsDTO.class)))
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", required = true),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)")
    })
    public Result<PageData<CouponsLogsDTO>> expirePage(@RequestParam Map<String, Object> params) {
        params.put("userId", ThreadLocalUtil.getUserId());
        params.put("available", false);
        params.put("status", CouponsStatus.START.getCode());
        PageData<CouponsLogsDTO> page = couponsLogsService.page(params);
        return new Result<PageData<CouponsLogsDTO>>().ok(page);
    }
}